package com.aismy.admin.controller;

import com.aismy.admin.entity.ConsultationMessage;
import com.aismy.admin.entity.ConsultationRecord;
import com.aismy.admin.service.ConsultationService;
import com.aismy.admin.utils.SecurityUtils;
import com.aismy.admin.vo.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin/consultation")
@RequiredArgsConstructor
public class ConsultationController {

    private final ConsultationService consultationService;

    @PostMapping("/create")
    public Result<ConsultationRecord> createConsultation(@RequestBody Map<String, Long> params) {
        Long consultantId = params.get("consultantId");
        Long userId = SecurityUtils.getUserId();
        return Result.success(consultationService.createConsultation(consultantId, userId));
    }

    @PostMapping("/{id}/end")
    public Result<Void> endConsultation(@PathVariable Long id) {
        consultationService.endConsultation(id);
        return Result.success();
    }

    /**
     * 发送咨询消息
     */
    @PostMapping("/message/send")
    public Result<ConsultationMessage> sendMessage(@RequestBody ConsultationMessage message) {
        consultationService.sendMessage(message);
        return Result.success();
    }

    /**
     * 标记消息为已读
     */
    @PostMapping("/messages/read")
    public Result<Void> markAsRead(@RequestBody List<Long> messageIds) {
        consultationService.markAsRead(messageIds);
        return Result.success();
    }

    /**
     * 获取咨询历史消息
     */
    @GetMapping("/{consultationId}/messages")
    public Result<Page<ConsultationMessage>> getHistoryMessages(
            @PathVariable Long consultationId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size) {
        return Result.success(consultationService.getHistoryMessages(consultationId, page, size));
    }


    @GetMapping("/user")
    public Result<Page<ConsultationRecord>> getUserConsultations(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Long userId = SecurityUtils.getUserId();
        return Result.success(consultationService.getUserConsultations(userId, page, size));
    }

    @GetMapping("/consultant")
    public Result<Page<ConsultationRecord>> getConsultantConsultations(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Long consultantId =  SecurityUtils.getUserId();
        return Result.success(consultationService.getConsultantConsultations(consultantId, page, size));
    }

    @GetMapping("/{id}")
    public Result<ConsultationRecord> getConsultation(@PathVariable Long id) {
        return Result.success(consultationService.getById(id));
    }
} 